package com.cloudptt.api.product.operator;

import com.cloudptt.api.comm.CauseCont;
import com.cloudptt.api.product.func.SipOperator;
import com.cloudptt.api.product.log.Log;
import com.mcptt.config.model.McIdInfo;
import com.mcptt.db.DBAdapter;
import com.mcptt.db.model.SessionInfo;
import com.mcptt.db.model.UserInfo;

import android.content.Context;
import android.os.RemoteException;

public class SysMsgOperator extends SipOperator
{
	 private static final String THIS_FILE = "SysOperator";
	 public static final String SysMsgType_Organizing_Desc = "self_organizing_group";
	 public static final String SysMsgType_Del_Desc = "self_del_group";
	 public static final String GroupName_Desc = "group_name";
	 public static final String GroupId_Desc = "group_id";
	 public static final String Members_Desc = "members";

	 private Context ctx = null;
	 private McIdInfo mcIdInfo = null;
	 private DBAdapter dbOperater = null;
	 
	public SysMsgOperator(Context ctx, McIdInfo mcIdInfo, DBAdapter dbOperater)
	{
		super(ctx);
		Log.d(THIS_FILE, "SysMsgOperator");
		this.ctx = ctx;
		this.mcIdInfo = mcIdInfo;
		this.dbOperater = dbOperater;
	}

	public int sendChPwdSysMsg(String oldPwd,String pwd)
	{
		Log.d(THIS_FILE, "sendChPwdSysMsg oldPwd = " + oldPwd + " pwd = " + pwd);
		String msgStr = "";//sysMsgCreator.modifyPasswordMsg(oldPwd, pwd);
		return sendSysMsg(msgStr);
	}

	public int sendQryNewUserMsg(String num)
	{
		Log.d(THIS_FILE, "sendQryNewUserMsg num = " + num);
		String msgStr = "";//sysMsgCreator.qryNewUserMsg(num);
		return sendSysMsg(msgStr);
	}
	
	public int sendPublish(String groupNum)
	{
		Log.d(THIS_FILE, "sendPublish groupNum = " + groupNum);
		int result = checkFirst();
		if(CauseCont.OperateCheck_Suc != result)
		{
			return result;
		}
		try {
			result = service.sendPublish(groupNum);
		} catch (RemoteException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return result;
	}
	
	public int sendSelfOrganizingGroupMsg(String name, String result)
	{
		Log.d(THIS_FILE, "sendSelfOrganizingGroupMsg name = " + name + " result = " + result);
		if(name == null
				|| name.length() <= 0)
		{
			return CauseCont.OperateCheck_Sys_OperateException;
		}
		if(result == null
				|| result.length() <= 0)
		{
			return CauseCont.OperateCheck_Sys_OperateException;
		}
		StringBuffer strBuf = new StringBuffer(SysMsgType_Organizing_Desc);
		strBuf.append(";");
		strBuf.append(GroupName_Desc).append("=").append(name).append(";");
		strBuf.append(Members_Desc).append("=").append(result);
		return sendSysMsg(strBuf.toString());
	}
	
	public int sendSelfDelGroupMsg(String id, String result)
	{
		Log.d(THIS_FILE, "sendSelfDelGroupMsg id = " + id + " result = " + result);
		if(id == null
				|| id.length() <= 0)
		{
			return CauseCont.OperateCheck_Sys_OperateException;
		}
		if(result == null
				|| result.length() <= 0)
		{
			return CauseCont.OperateCheck_Sys_OperateException;
		}
		StringBuffer strBuf = new StringBuffer(SysMsgType_Del_Desc);
		strBuf.append(";");
		strBuf.append(GroupId_Desc).append("=").append(id).append(";");
		strBuf.append(Members_Desc).append("=").append(result);
		return sendSysMsg(strBuf.toString());
	}

	private int sendSysMsg(String contentStr)
	{
		Log.d(THIS_FILE, "sendSysMsg contentStr = " + contentStr);
		int result = checkFirst();
		if(CauseCont.OperateCheck_Suc != result)
		{
			return result;
		}
		try {
			result = service.sendSystemMessage(contentStr);
			if(result == CauseCont.OperateCheck_Suc) 
			{
//				UserInfo sysInfo = UserInfo.toSysInfo();
//				String mcId = mcIdInfo.getMcId();
//				SessionInfo session = new SessionInfo(sysInfo,mcId,SessionInfo.SessionType_Sys,contentStr,0);
//				dbOperater.insertNewSession(session,true);
			}
		} catch (RemoteException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return CauseCont.OperateCheck_Suc;
	}
	
//	public int sendPublish()
//	{
//		Log.d(THIS_FILE, "sendPublish");
//		int result = checkFirst();
//		if(CauseCont.OperateCheck_Suc != result)
//		{
//			return result;
//		}
//		try {
//			result = service.sendPublish();
//		} catch (RemoteException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		}
//		return result;
//	}
}
